//
//  UserFeedBackViewController.m
//  HuLaQuan
//
//  Created by hok on 1/15/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "UserFeedBackViewController.h"

@interface UserFeedBackViewController ()


@property (weak, nonatomic) IBOutlet UITextView *feedbackInput;

@property (weak, nonatomic) IBOutlet UIButton *feedbackSend;

@end

@implementation UserFeedBackViewController

- (void)setupUI
{
    //   // self.feedbackInput.layer.borderColor = [UIColorFromRGBA(0x438036, 1.0) CGColor];
    //    self.feedbackInput.layer.borderWidth = 1.0f;
    //    self.feedbackInput.layer.cornerRadius = 6.0f;
    self.title = @"用户反馈";
    self.feedbackInput.text = kUserFeedbackPlaceholder;
    
    self.feedbackInput.font = [UIFont fontWithName:FONT_NAME size:14];
    
    
    
    self.feedbackSend.enabled = NO;
    
    
    [self setupNavBackButton];
    
    [self setupTapGesture];
    
    
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:kUserFeedbackPlaceholder]) {
        textView.text = @"";
    }
    
}

/*
 检查输入了的字数，限制上限120字，字数为0时
 
 */
-(void)textViewDidChange:(UITextView *)textView  {
    
    if (textView.text.length > 120) {
        // textView.text = placeHolder;
        textView.text = [textView.text substringToIndex:120];
        [textView resignFirstResponder];
    }
    if (textView.text.length > 0 && ![textView.text  isEqual: kUserFeedbackPlaceholder]) {
        self.feedbackSend.enabled = YES;
    }else{
        self.feedbackSend.enabled = NO;
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [textView resignFirstResponder];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    
    return YES;
}
- (NSUInteger) lenghtWithString:(NSString *)string
{
    NSUInteger len = string.length;
    // 汉字字符集
    NSString * pattern  = @"[\u4e00-\u9fa5]";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    // 计算中文字符的个数
    NSInteger numMatch = [regex numberOfMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, len)];
    
    return len + numMatch;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length < 1) {
        textView.text = kUserFeedbackPlaceholder;
    }
    
}

- (IBAction)TapFeedBackSendButton:(UIButton *)sender
{
    [self.feedbackInput endEditing:YES];
    [self.feedbackInput resignFirstResponder];
    [self submintFeedBack:self.feedbackInput.text];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    
    //[self.feedbackInput endEditing:YES];
}
- (void)submintFeedBack:(NSString *)feedbackText
{
   /* NSMutableString *value = [[NSMutableString alloc] init];
    [value appendFormat:@"%@%@/%@/",BaseURLOfFeedBack,[CurrentUser sharedInstance].ID,[CurrentUser sharedInstance].token];
    
    ////NSLog(@"%@ %@",value,info);
    //for test
    
    NSDictionary *info = @{@"userid":[CurrentUser sharedInstance].ID,@"feedback":feedbackText};
    NSString *urlString = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    NSMutableDictionary * mutaDic = [NSMutableDictionary dictionaryWithDictionary:info];
    [mutaDic setObject:[CurrentUser sharedInstance].ID forKey:@"userid"];
    
    [AFNetWorkEngine postJSONWithUrl:urlString parameters:[mutaDic copy] success:^(BOOL isSuccess, id item){
        
        [KVNProgress dismiss];
        NSDictionary *result;
        if (isSuccess) {
            
            [MobClick event:@"feedback" attributes:@{@"content":feedbackText}];
            
            UIAlertController *x = [UIAlertController alertWithTitle:@"提交反馈" andMessage:@"您填写的反馈信息已经提交给余物宝官方客服，我们会尽快改善，谢谢" andBlock:^(UIAlertAction *action){
                //NSLog(@"%@",action.title);
                [self.navigationController popViewControllerAnimated:YES];
                
            } type:@"confirm"];
            [self presentViewController:x animated:YES completion:nil];
            
        }else{
            //result = @{@"isSuccess":@NO,@"message":item};
        }
        
    }];*/
    
}


@end
